class Site < ActiveRecord::Base
  has_many :identities
  
  def recent_identities(number = 10)
    identities.sort{|a, b| b.created_at <=> a.created_at }[0..number-1]
  end
  
  def self.all_approved
    find(:all, :conditions => ["approved = true"], :order => 'id')
  end

  def self.all_unapproved
    find(:all, :conditions => ["approved <> true"])
  end
  
  def self.most_popular(number = 10)
    # could implement with children counter
    # refer to 362 of "Agile Web Development with Rails" (2nd edition)
    order_clause = "(select count(*) from identities where id in ("
    order_clause = order_clause + "select id from identities where site_id = sites.id)) DESC"
    find(:all, :limit => number, :order => order_clause)
  end
end
